﻿// Copyright 2010 Maxime Serrano <lastchronicler@gmail.com>
// Copyright 2009, 2010 Matvei Stefarov <me@matvei.org>
using System;
using System.Text;
using System.Net;

namespace ForgeCraft
{
    public sealed class WindowInfo
    {
        public const int fieldCount = 8;

        public string location;
        public DateTime createdDate;
        public byte windowType;
        public string ownerName;
        public bool isPublic;
        public string allowedPlayers;
        public string inventory;
        public string additional;

        // === Serialization & Defaults =======================================
        // fabricate info for a player
        public WindowInfo(BlockPosition bp, World world)
        {
            string loc = bp.x + ":" + bp.y + ":" + bp.z;
            
            location = loc;
            createdDate = DateTime.UtcNow;

            ChunkBlock cb = world.GetBlock(bp.x, bp.y, bp.z);
            
            switch ((Block)cb.type)
            {
                case Block.Chest: 
                    windowType = 0; break;
                case Block.Furnace: 
                case Block.BurningFurnace:
                    windowType = 2; break;
                case Block.Workbench: 
                    windowType = 1; break;
                case Block.Dispenser: 
                    windowType = 3; break;
                default:
                    windowType = 0; break;
            }

            ownerName = "-";
            isPublic = true;
            allowedPlayers = "-";
            inventory = "-";
            additional = "0:0:0";
        }

        // load info from file
        public WindowInfo(string[] fields)
        {
            location = fields[0];
            createdDate = DateTime.Parse(fields[1]);
            windowType = byte.Parse(fields[2]);
            ownerName = fields[3];
            isPublic = bool.Parse(fields[4]);
            allowedPlayers = fields[5];
            inventory = fields[6];
            additional = fields[7];
        }


        // save to file
        public string Serialize()
        {
            string[] fields = new string[fieldCount];

            fields[0] = location;
            fields[1] = createdDate.ToString();
            fields[2] = windowType.ToString();
            fields[3] = ownerName;
            fields[4] = isPublic.ToString();
            fields[5] = allowedPlayers;
            fields[6] = inventory;
            fields[7] = additional;

            return String.Join(",", fields);
        }


        // === Updating =======================================================

        // update information
        public void WindowSet(Window window)
        {
            //TODO : Check if [WindowInfo] also needs passed

            //slot,item,count,damage|slot,item,count,damage|slot,item,count,damage

            //Generic Stuff
            window.createdDate = createdDate;
            window.isPublic = isPublic;
            window.allowedPlayers = allowedPlayers;
            window.ownerName = ownerName;

            string[] invItemsList = this.inventory.Split('|');

            foreach (string s in invItemsList)
            {
                if (s.Length > 2)
                {
                    string[] invItem = s.Split(':');

                    short slot = -1, type = 0, damage = 0;
                    byte count = 0;

                    slot = short.Parse(invItem[0]);
                    type = short.Parse(invItem[1]);
                    count = byte.Parse(invItem[2]);
                    damage = short.Parse(invItem[3]);

                    if (slot < 0)
                    {
                        Logger.Log("Window: Could not parse slot for window {0}:{1}.).", LogType.Error, this.location, invItem[0]);
                        break;
                    }

                    if (type == 0 || count == 0)
                    {
                        Logger.Log("Window: Invalid item for window {0}:{1}:{2}.).", LogType.Error, this.location, invItem[1], invItem[2]);
                        break;
                    }

                    window.slots[slot] = new InventoryItem(type, count, damage);
                }
            }

            window.additional = additional;
            
            //Prefill empty slots with null, stops the client crashing.
            for (short i = 0; i < window.slots.Length; i++)
            {
                if (window.slots[i].Type < 1)
                {
                    window.slots[i].Clear();
                }
            }
        }
    }
}